
const PORT = 8000;
const { Pool } = require('pg');
const pool = new Pool({
    user: 'postgres',
    host: 'project_data_base',
    //host: '127.0.0.1',
    database: 'postgres',
    password: '1234',
    port: 5432,
  });
class types {
  /**
     * @swagger
     * components:
     *   schemas:
     *     Type:
     *       type: object
     *       properties:
     *         id:
     *           type: integer
     *         name:
     *           type: string
     *     Subtype:
     *       type: object
     *       properties:
     *         id:
     *           type: integer
     *         name:
     *           type: string
     *         type_id:
     *           type: integer
     * 
     * /types:
     *   get:
     *     tags: [Оборудование]
     *     summary: Получение списка типов
     *     responses:
     *       200:
     *         description: Список типов
     *         content:
     *           application/json:
     *             schema:
     *               type: object
     *               properties:
     *                 typess:
     *                   type: array
     *                   items:
     *                     $ref: '#/components/schemas/Type'
     *       500:
     *         description: Внутренняя ошибка сервера
     */
    async typesGet (req, res) {
          try {
        
            const queryText = `
              SELECT * FROM types;
            `;
        
            const result = await pool.query(queryText);
            const typess = result.rows;
        
        
            const response = {
                typess ,
            };
        
            res.json(response);
          } catch (error) {
            res.status(500).json({ error: error.message });
          }
        }
    /**
     * @swagger
     * /types:
     *   post:
     *     tags: [Оборудование]
     *     summary: Создание нового типа
     *     requestBody:
     *       required: true
     *       content:
     *         application/json:
     *           schema:
     *             type: object
     *             properties:
     *               name:
     *                 type: string
     *     responses:
     *       200:
     *         description: Новый тип создан
     *         content:
     *           application/json:
     *             schema:
     *               $ref: '#/components/schemas/Type'
     *       500:
     *         description: Внутренняя ошибка сервера
     */
    async typesPost (req, res){
          try {
            const { name } = req.body;
            
            const queryText = `
              INSERT INTO types (tname)
              VALUES ($1)
              RETURNING *;
            `;
            const values = [name];
        
            const result = await pool.query(queryText, values);
            const newtypes = result.rows[0];
        
            res.json(newtypes);
          } catch (error) {
            res.status(500).json({ error: error.message });
          }
        }
        async typesDelete (req, res){
          try {
            const { id } = req.query;
            
            
            const queryText = `
              Delete from types 
              where id=$1
              RETURNING *;
            `;
            const values = [id];
        
            const result = await pool.query(queryText, values);
            
            const newtypes = result.rows[0];
            console.log(newtypes)
            res.json(newtypes);
          } catch (error) {
            res.status(500).json({ error: error.message });
          }
        }
    /**
     * @swagger
     * /subtypes:
     *   get:
     *     tags: [Оборудование]
     *     summary: Получение списка подтипов
     *     parameters:
     *       - in: query
     *         name: type_id
     *         schema:
     *           type: integer
     *         description: Идентификатор типа для фильтрации
     *     responses:
     *       200:
     *         description: Список подтипов
     *         content:
     *           application/json:
     *             schema:
     *               type: object
     *               properties:
     *                 subtypes:
     *                   type: array
     *                   items:
     *                     $ref: '#/components/schemas/Subtype'
     *       500:
     *         description: Внутренняя ошибка сервера
     */
    async subTypesGet (req, res) {
        try {
            const types = req.query.types || null
            const values = [];
            var queryText = `
            SELECT * FROM subtypes where 1=1
            `;
            if (types !== null) {
              queryText += `AND type_id = $1`;
              values.push(types)
            }
            const result = await pool.query(queryText, values);
            const subtypes = result.rows;
        
        
            const response = {
                subtypes ,
            };
        
            res.json(response);
        } catch (error) {
            res.status(500).json({ error: error.message });
        }
        }
    /**
     * @swagger
     * /subtypes:
     *   post:
     *     tags: [Оборудование]
     *     summary: Создание нового подтипа
     *     requestBody:
     *       required: true
     *       content:
     *         application/json:
     *           schema:
     *             type: object
     *             properties:
     *               name:
     *                 type: string
     *               type_id:
     *                 type: integer
     *     responses:
     *       200:
     *         description: Новый подтип создан
     *         content:
     *           application/json:
     *             schema:
     *               $ref: '#/components/schemas/Subtype'
     *       500:
     *         description: Внутренняя ошибка сервера
     */    
    async subTypesPost (req, res){
        try {
            const { name, type_id } = req.body;
        
            const queryText = `
            INSERT INTO subtypes (name, type_id)
            VALUES ($1, $2)
            RETURNING *;
            `;
            const values = [name, type_id];
        
            const result = await pool.query(queryText, values);
            const newtypes = result.rows[0];
        
            res.json(newtypes);
        } catch (error) {
            res.status(500).json({ error: error.message });
        }
        }
        async subTypesDelete (req, res){
          try {
            const { id } = req.query;
            
            
            const queryText = `
              Delete from subtypes 
              where id=$1
              RETURNING *;
            `;
            const values = [id];
        
            const result = await pool.query(queryText, values);
            
            const newtypes = result.rows[0];
            console.log(newtypes)
            res.json(newtypes);
          } catch (error) {
            res.status(500).json({ error: error.message });
          }
        }
}

module.exports = new types()